home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / terms / tip / tipout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-13  |  3.2 KB  |  143 lines

  1. /*
  2.  * Copyright (c) 1983 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #ifndef lint
  19. static char sccsid[] = "@(#)tipout.c    5.2 (Berkeley) 9/2/88";
  20. #endif /* not lint */
  21.  
  22. #include "tip.h"
  23. /*
  24.  * tip
  25.  *
  26.  * lower fork of tip -- handles passive side
  27.  *  reading from the remote host
  28.  */
  29.  
  30. static    jmp_buf sigbuf;
  31.  
  32. /*
  33.  * TIPOUT wait state routine --
  34.  *   sent by TIPIN when it wants to posses the remote host
  35.  */
  36. sigfunc_t
  37. intIOT()
  38. {
  39.  
  40.     write(repdes[1],&ccc,1);
  41.     read(fildes[0], &ccc,1);
  42.     longjmp(sigbuf, 1);
  43. }
  44.  
  45. /*
  46.  * Scripting command interpreter --
  47.  *  accepts script file name over the pipe and acts accordingly
  48.  */
  49. sigfunc_t
  50. intEMT()
  51. {
  52.     char c, line[256];
  53.     register char *pline = line;
  54.     char reply;
  55.  
  56.     read(fildes[0], &c, 1);
  57.     while (c != '\n') {
  58.         *pline++ = c;
  59.         read(fildes[0], &c, 1);
  60.     }
  61.     *pline = '\0';
  62.     if (boolean(value(SCRIPT)) && fscript != NULL)
  63.         fclose(fscript);
  64.     if (pline == line) {
  65.         boolean(value(SCRIPT)) = FALSE;
  66.         reply = 'y';
  67.     } else {
  68.         if ((fscript = fopen(line, "a")) == NULL)
  69.             reply = 'n';
  70.         else {
  71.             reply = 'y';
  72.             boolean(value(SCRIPT)) = TRUE;
  73.         }
  74.     }
  75.     write(repdes[1], &reply, 1);
  76.     longjmp(sigbuf, 1);
  77. }
  78.  
  79. sigfunc_t
  80. intTERM()
  81. {
  82.  
  83.     if (boolean(value(SCRIPT)) && fscript != NULL)
  84.         fclose(fscript);
  85.     finish();
  86. }
  87.  
  88. sigfunc_t
  89. intSYS()
  90. {
  91.  
  92.     boolean(value(BEAUTIFY)) = !boolean(value(BEAUTIFY));
  93.     longjmp(sigbuf, 1);
  94. }
  95.  
  96. /*
  97.  * ****TIPOUT   TIPOUT****
  98.  */
  99. tipout()
  100. {
  101.     char buf[BUFSIZ];
  102.     register char *cp;
  103.     register int cnt;
  104.     extern int errno;
  105.     int omask;
  106.  
  107.     signal(SIGINT, SIG_IGN);
  108.     signal(SIGQUIT, SIG_IGN);
  109.     signal(SIGEMT, intEMT);        /* attention from TIPIN */
  110.     signal(SIGTERM, intTERM);    /* time to go signal */
  111.     signal(SIGIOT, intIOT);        /* scripting going on signal */
  112.     signal(SIGHUP, intTERM);    /* for dial-ups */
  113.     signal(SIGSYS, intSYS);        /* beautify toggle */
  114.     (void) setjmp(sigbuf);
  115.     for (omask = 0;; sigsetmask(omask)) {
  116.         cnt = read(FD, buf, BUFSIZ);
  117.         if (cnt <= 0) {
  118.             /* lost carrier */
  119.             if (cnt < 0 && errno == EIO) {
  120.                 sigblock(sigmask(SIGTERM));
  121.                 intTERM();
  122.                 /*NOTREACHED*/
  123.             }
  124.             continue;
  125.         }
  126. #define    ALLSIGS    sigmask(SIGEMT)|sigmask(SIGTERM)|sigmask(SIGIOT)|sigmask(SIGSYS)
  127.         omask = sigblock(ALLSIGS);
  128.         for (cp = buf; cp < buf + cnt; cp++)
  129.             *cp &= 0177;
  130.         write(1, buf, cnt);
  131.         if (boolean(value(SCRIPT)) && fscript != NULL) {
  132.             if (!boolean(value(BEAUTIFY))) {
  133.                 fwrite(buf, 1, cnt, fscript);
  134.                 continue;
  135.             }
  136.             for (cp = buf; cp < buf + cnt; cp++)
  137.                 if ((*cp >= ' ' && *cp <= '~') ||
  138.                     any(*cp, value(EXCEPTIONS)))
  139.                     putc(*cp, fscript);
  140.         }
  141.     }
  142. }
  143.